<?php
	include "configure.php";
	
	htmlhead("添加元件");
	
	htmlheadend();
	
?>

<?php 
	if(isset($_GET['posted'])) {
		if($_GET['posted'] == "insert") {
			if($_GET['type'] == "new") {
				$query = "insert into comptype (name) values ('$_GET[newcomptype]')";
				if(!mysql_query($query)) die("dabiaozi，database 出错！！！");
				else {
					$query = "select * from comptype where name='$_GET[newcomptype]'";
					$result = mysql_query($query);
					if($row = mysql_fetch_array($result)) {
						$type = $row['uid'];
					}
					else die("<span class=\"error\">dabiaozi，database 出错！！！</span>");
				}
			}
			else $type = $_GET['type'];
			$sql = "insert into components (name, type, value, unit, description, num, position) values ('$_GET[name]','$type','$_GET[value]','$_GET[unit]','$_GET[description]','$_GET[num]','$_GET[position]')";
			//echo $sql;
			if(!mysql_query($sql)) die("<span class=\"error\">dabiaozi，database 出错！！！</span>");
			else echo "添加成功";
		}
		else if($_GET['posted'] == "update") {
			if($_GET['type'] == "new") {
				$query = "insert into comptype (name) values ('$_GET[newcomptype]')";
				if(!mysql_query($query)) die("dabiaozi，database 出错！！！");
				else {
					$query = "select * from comptype where name='$_GET[newcomptype]'";
					$result = mysql_query($query);
					if($row = mysql_fetch_array($result)) {
						$type = $row['uid'];
					}
					else die("<span class=\"error\">dabiaozi，database 出错！！！</span>");
				}
			}
			else $type = $_GET['type'];
			$sql = "update components set name = $_GET[name], type = $type, value = $_GET[value], unit = $_GET[unit], description = $_GET[description], num = $_GET[num], position = $_GET[position] where uid = $_GET[uid]";
			//echo $sql;
			if(!mysql_query($sql)) die("<span class=\"error\">dabiaozi，database 出错！！！</span>");
			else echo "添加成功";
		}
	}
?>
<a class="index" href="index.php">主页</a>
<div id="mainform">
	<form action="" method="get">
		<div>
			<?php
			if(isset($_GET['posted']) && $_GET['posted'] == "update") {
				// TODO 修改元件信息
			}
			?>
			<label for="name">元件名：</label>
			<input name="name" id="name" type="text" value="<?php if(isset($_GET["name"])) echo $_GET["name"]; ?>" />
		</div>
		<div>
			<label for="type">元件类型：</label>
			<select name="type" id="type">
				<option value="new">新建</option>
			<?php 
				$sql = "select * from comptype";
				$result = mysql_query($sql);
				while ($row = mysql_fetch_array($result)) {
					if(isset($_GET["type"])) {
						if($_GET["type"] == $row["uid"]) echo "<option value=\"$row[uid]\" selected=\"selected\" >$row[name]</option>";
					}
					else echo "<option value=\"$row[uid]\">$row[name]</option>";
				}
			?>
			</select>
			<input type="text" name="newcomptype" />
		</div>
		<div>
			<label for="value">值：</label>
			<input type="text" name="value" id="value" value="<?php if(isset($_GET["value"])) echo $_GET["value"]; ?>" />
		</div>
		<div>
			<label for="num">数量：</label>
			<input type="text" name="num" id="num" value="<?php if(isset($_GET["num"])) echo $_GET["num"]; ?>" />
		</div>
		<div>
			<label for="unit">单位：</label>
			<input type="text" name="unit" id="unit" value="<?php if(isset($_GET["unit"])) echo $_GET["unit"]; ?>" />
		</div>
		<div>
			<label for="position">位置（保管人）：</label>
			<input type="text" name="position" id="position" value="<?php if(isset($_GET["position"])) echo $_GET["position"]; ?>" />
		</div>
		<div>
			<label for="description">描述：</label>
			<textarea name="description" rows="10" cols="50"></textarea>
		</div>
		<input type="hidden" name="posted" value="insert" />
		<input type="submit" value="添加" class="yage_button" />
	</form>
</div>

<?php 
	htmltail();
?>